<?php

/*
	@version SVN: $Id: db_wrapper.php 2 2007-10-12 16:56:15Z desolator $

	Copyright (C) 2007 The ActySofts Organization

	This file is part of Sampi.

	Sampi is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.

	Sampi is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with SauthSQL; if not, write to the Free Software
	Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

/*
	@description This is the database object. It will handle all the database stuff.

	@see The other database plug-ins.

	@todo Complete it with all required functions.

	@author Desolator
*/
class DataBase
{
	// Declare basic variables
	public $DB_System;
	public $DB_Hanlde;
	private $DB_Functions;

	// Declare connection info variables
	public $DB_Name;
	public $DB_Server;
	private $DB_Username;
	private $DB_Password;

	// Declare settings variables
	public $DB_Settings[];

	/*
		@description This constructor will be called when the object is created.

		@arg $DB_System The database system to use (e.g. MySql, PostreSQL, OBDC, etc.)

		@arg $DB_Server The database server to connect to.

		@arg $DB_Name The name of the database to use.

		@arg $DB_Username The database username that will be used to connect.

		@arg  $DB_Password The password of the database username.

		@todo Add everything that's needed to be done automatically when the object is created.

		@author Desolator
	*/
	function __construct($DB_System, $DB_Server, $DB_Name, $DB_Username, $DB_Password)
	{
		// Set all the database info
		$this->DB_System = $DB_System;
		$this->DB_Name = $DB_Name;
		$this->DB_Server = $DB_Server;
		$this->DB_Username = $DB_Username;
		$this->DB_Password = $DB_Password;
		
		// Get the database functions
		$this->DB_Functions = new $this->DB_System();

		// Get the database settings
		$this->get_settings()

		// Connect to the database server
		$this->connect();

		// Select the database
		$this->select_db();
	}


	/*
		@description This function will get all the required database settings.

		@todo Add all the required settinga to the array.

		@author Desolator
	*/
	function get_settings()
	{
		// Find out whether to use persistent connections or not
		$this->DB_Settings['KeepConnAlive'] = todo();
	}

	/*
		@description This function will select a database that will be used.

		@author Desolator
	*/
	function select_db()
	{
		// Select the database
		$this->DB_Functions->select_db($this->DB_Name, $this->DB_Hanlde);
	}

	/*
		@description This function will connect to a database server.

		@author Desolator
	*/
	function connect()
	{
		// If we are not using persistent connections
		if ($this->DB_Settings['KeepConnAlive'] == false) {
			// Connect to the database server
			$this->DB_Hanlde = $this->DB_Functions->connect($this->DB_Server, $this->DB_Username, $this->DB_Password);
		} else {
			// Connect to the database server and keep the connection alive
			$this->DB_Hanlde = $this->DB_Functions->pconnect($this->DB_Server, $this->DB_Username, $this->DB_Password);
		}
	}

	/*
		@description This function will escape special characters in a string for use in a SQL statement.

		@author Desolator
	*/
	function escape_string($SQL_Statement)
		// Escape the special characters
		return $this->DB_Functions->escape_string($SQL_Statement);
	}

	/*
		@description This function will send a MySQL query for processing.

		@author Desolator
	*/
	function query($SQL_Statement)
		// Escape the special characters
		return $this->DB_Functions->query($SQL_Statement, $this->DB_Hanlde);
	}


	/*
		@description This function will close the connection to a database server.

		@author Desolator
	*/
	function close()
	{
		// If we are not using persistent connections
		if ($this->DB_Settings['KeepConnAlive'] == false) {
			// Close the non-persistent connection
			$this->DB_Functions->close($this->DB_Hanlde);
		}
	}

	/*
		@description This destructor is used to destroy the object and free server resources.

		@author Desolator
	*/
	function __destruct()
	{
		// Destroy the object
		unset($this);
	}
}

?>